package com.superm.service.impl;

import com.alibaba.fastjson.JSON;
import com.superm.entity.*;
import com.superm.mappers.GoodsMapper;
import com.superm.mappers.StorgageRackMapper;
import com.superm.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Transactional(propagation = Propagation.REQUIRED)
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public Goods allGoods(Goods goods) {
        return goodsMapper.allGoods(goods);
    }
    @Autowired
    private GoodsMapper goodsMp;

    @Autowired
    private StorgageRackMapper storgageRackMp;
    /**
     * 实现模糊查询商品信息
     * @return
     */
    @Override
    public List<Goods> selectLikeGoods(GoodsPoJo goodsPoJo, Integer pageNo, Integer pageSize){
        Goods goods = new Goods();
        //赋值
        BeanUtils.copyProperties(goodsPoJo,goods);
        //品牌赋值
        goods.setBrand(new Brand(goodsPoJo.getBrand(),null));
        //货架赋值
        goods.setStorgageRack(new StorgageRack(goodsPoJo.getStorgageRack()));
        //一级分类赋值
        goods.setRelationShipOne(new RelationShip(goodsPoJo.getRelationShipOne(),null,null,null));
        return goodsMp.selectAll(goods,(pageNo-1)*pageSize,pageSize);
    }

    /**
     * 实现货架编号查询底下的商品信息
     * @param storgageRack
     * @return
     */
    @Override
    public Map<String,Object> selectStorgageRackId(Integer storgageRack){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Goods> goodsList =  goodsMapper.selectMationInfo(storgageRack);
        map.put("code",0);
        map.put("count",0);
        map.put("data",goodsList);
        return map;
    }



    /**
     * 实现修改商品信息
     * @param file
     * @param goodsPoJo
     * @param request
     * @return
     * @throws IOException
     */
    @Override
    public Map<String, Object> updateGoods(MultipartFile file, GoodsPoJo goodsPoJo, HttpServletRequest request) throws IOException {
        Map<String,Object> map = new HashMap<String,Object>();
        Goods goods = new Goods();
        //赋值
        BeanUtils.copyProperties(goodsPoJo,goods);
/*
        goods.setStorgageRack(new StorgageRack(goodsPoJo.getStorgageRack(),null,null,null,null));
*/
        goods.setBrand(new Brand(goodsPoJo.getBrand(),null));
        goods.setRelationShipOne(new RelationShip(goodsPoJo.getRelationShipOne(),null,null,null));
        goods.setRelationShipTwo(new RelationShip(goodsPoJo.getRelationShipTwo(),null,null,null));
        //获取当前路径的绝对路径
        String url = request.getServletContext().getRealPath("static/images");
        //获取后缀
        String name =  file.getOriginalFilename();
        System.out.println(url+"/"+name);
        //保存至文件
        file.transferTo(new File(url+"/"+name));
        //路径添加数据库
        goods.setGoodsImg("static/images"+"/"+name);
        //添加商品信息
        if (goodsMp.updateMation(goods)>0){
            map.put("code",0);
            map.put("success",true);
            map.put("msg","商品修改成功！");
            return  map;
        }
        return null;
    }

    /**
     * 实现查询条数
     * @param goodsPoJo
     * @return
     */
    @Override
    public Integer selectGoodsCount(GoodsPoJo goodsPoJo) {
        Goods goods = new Goods();
        //赋值
        BeanUtils.copyProperties(goodsPoJo,goods);
        //品牌赋值
        goods.setBrand(new Brand(goodsPoJo.getBrand(),null));
        //一级分类赋值
        goods.setRelationShipOne(new RelationShip(goodsPoJo.getRelationShipOne(),null,null,null));
        return goodsMp.selectCount(goods);
    }

    /**
     * 实现添加商品信息
     * @param file
     * @param goodsPoJo
     * @return
     */
    @Override
    public Map<String,Object> addGoods(MultipartFile file, GoodsPoJo goodsPoJo, HttpServletRequest request) throws IOException {
        Map<String,Object> map = new HashMap<String,Object>();
        Goods goods = new Goods();
        //赋值
        BeanUtils.copyProperties(goodsPoJo,goods);
/*
        goods.setStorgageRack(new StorgageRack(goodsPoJo.getStorgageRack(),null,null,null,null));
*/
        goods.setBrand(new Brand(goodsPoJo.getBrand(),null));
        goods.setRelationShipOne(new RelationShip(goodsPoJo.getRelationShipOne(),null,null,null));
        goods.setRelationShipTwo(new RelationShip(goodsPoJo.getRelationShipTwo(),null,null,null));
/*        //先查询货架的容量是否足够装下该商品
        StorgageRack storgageRack = storgageRackMp.selectIdInfoMation(goodsPoJo.getStorgageRack());
        //算出货架剩余容量
        double remain = storgageRack.getGoorssCapacity()-storgageRack.getResidualCapacity();
        //计算商品所需容量
        double goodsNeed = goodsPoJo.getCapacity()*goodsPoJo.getGoodsQuantity();
        System.out.println(goods);
        //判断是否能装下该商品
        if (goodsNeed>remain){
            return null;
        }*/
        //容量充足 增加货架的已用容量 进行商品的添加
        /*        if (storgageRackMp.minusCaoacity(goodsPoJo.getStorgageRack(),goodsNeed)>0){*/
        //文件上传
        //获取当前路径的绝对路径
        String url = request.getServletContext().getRealPath("static/images");
        //获取后缀
        String name =  file.getOriginalFilename();
        System.out.println(url+"/"+name);
        //保存至文件
        file.transferTo(new File(url+"/"+name));
        //路径添加数据库
        goods.setGoodsImg("static/images"+"/"+name);
        //添加商品信息
        if (goodsMp.addInfoMation(goods)>0){
            map.put("code",0);
            map.put("success",true);
            map.put("msg","商品添加成功！");
            return  map;
        }

        return null;

    }

    @Override
    public List<Goods> allGoodSs() {
        return goodsMp.allGoodSs();
    }

    /**
     * 实现商品下架
     * @param goodsId
     * @return
     */
    @Override
    public Map<String, Object> goodsOffTheShelf(Integer goodsId) {
        Map<String,Object> map = new HashMap<String,Object>();
        //根据商品编号查询商品指定信息
        Goods goods =  goodsMapper.selectIdMationInfo(goodsId);
        System.out.println(goods);
        //计算商品共占用容量,对所在货架容量腾出位置
        Double  goodsCocity  = goods.getCapacity()*goods.getGoodsQuantity();
        System.out.println(goodsCocity);
        //对货架所占容量减少
        if (storgageRackMp.decreaseCaoacity(goods.getStorgageRack().getStorgageRackId(),goodsCocity)>0){
            //对商品下架,并且数量清空,所在货架清空
            if (goodsMp.goodsOffTheShelf(goodsId)>0){
                map.put("code",0);
                map.put("success","true");
                map.put("msg","下架成功！");
            }

        }
        return map;
    }

    /**
     * 实现根据商品编号查询商品信息
     * @param goodsId
     * @return
     */
    @Override
    public Goods selectIdGoods(Integer goodsId) {
        Goods goods  = goodsMapper.selectIdMationInfo(goodsId);
        return goods;
    }

    /**
     * 实现异步查询商品信息
     * @param goodsId
     * @return
     */
    @Override
    public Map<String, Object> ajaxSelectIdGoods(Integer goodsId) {
        Map<String,Object> map = new HashMap<String,Object>();
        Goods goods = goodsMapper.selectIdMationInfo(goodsId);
        map.put("data", JSON.toJSONString(goods));
        return map;
    }

    /**
     * 查询全部商品
     * @return
     */
    @Override
    public List<Goods> selectGoodSs() {
        return goodsMapper.selectGoodSs();
    }
}
